package com.example.service;

import cn.hutool.core.util.StrUtil;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.User;
import com.example.exception.CustomerException;
import com.example.mapper.UserMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public List<User> selectAll(User user){
        return userMapper.selectAll(user);
    }

    public PageInfo<User> selectPage(Integer pageNum, Integer pageSize,User user) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> list = userMapper.selectAll(user);
        return PageInfo.of(list);

    }

    public void add(User user) {
       User user1= userMapper.selectByUsername(user.getUsername());
       if(user1!=null){
           throw new CustomerException("账号重复");
       }
       if(StrUtil.isBlank(user.getPassword())){
           user.setPassword("123456");
       }
        if(StrUtil.isBlank(user.getName())){
            user.setName(user.getUsername());
        }
       user.setRole("USER");
        userMapper.insert(user);
    }

    public void update(User user) {
        userMapper.updateById(user);
    }

    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    public void deleteBatch(List<User> list) {
        for (User user : list) {
            userMapper.deleteById(user.getId());
        }
    }

    public User login(Account account) {
        User user1 = userMapper.selectByUsername(account.getUsername());
        if(user1==null){
            throw new CustomerException("账号或密码错误");
        }
        if(!user1.getPassword().equals(account.getPassword())){
            throw new CustomerException("账号或密码错误");
        }
        String token = TokenUtils.createToken(user1.getId() + "-" + "USER", user1.getPassword());
        user1.setToken(token);
        return user1;
    }

    public void register(User user) {
        User user1= userMapper.selectByUsername(user.getUsername());
        if(user1!=null){
            throw new CustomerException("账号重复");
        }
        if(StrUtil.isBlank(user.getPassword())){
            user.setPassword("123456");
        }
        user.setRole("USER");
        userMapper.insert(user);
    }

    public User selectById(String id) {
        return userMapper.selectById(id);
    }

    public void updatePassword(Account account) {
        if(!account.getNewPassword().equals(account.getNew2Password())){
            throw new CustomerException("500","您两次输入的密码不一致");
        }
        Account currentUser=TokenUtils.getCurrentUser();
        if(!account.getPassword().equals(currentUser.getPassword())){
            throw new CustomerException("500","原密码错误");
        }
        User user = userMapper.selectById(currentUser.getId().toString());
        user.setPassword(account.getNewPassword());
        userMapper.updateById(user);
    }
}
